package view.viewPanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import view.ChessFrame;
import view.GBCUtils;
import control.Communication;

/**
 * singleton class for the login-panel, initialized in the constructor of ChessFrame
 */
@SuppressWarnings( "serial" )
public class LoginViewPanel extends JPanel {

    /**
     * the only instance of this class
     */
    private static LoginViewPanel instance;

    /**
     * textfield for the user name
     */
    private JTextField tfUser;

    /**
     * passwordfield the user's password
     */
    private JPasswordField pwf;

    /**
     * reference to the ChessFrame-class
     */
    private ChessFrame chessFrame;

    /**
     * private constructor, calls super() from JPanel
     * 
     * @param frame
     *            reference to the ChessFrame-class
     */
    private LoginViewPanel( final ChessFrame frame )
    {
	super();
	this.chessFrame = frame;

	this.setLayout( new GridBagLayout() );

	JLabel lUser = new JLabel( "Benutzername" );
	JLabel lPW = new JLabel( "Passwort" );
	this.tfUser = new JTextField( "Jace" );
	this.tfUser.addFocusListener( new FocusListener() {

	    @Override
	    public void focusLost( FocusEvent arg0 )
	    {
		// nothing.. :3
	    }

	    @Override
	    public void focusGained( FocusEvent arg0 )
	    {
		( (JTextField)arg0.getSource() ).selectAll();
	    }
	} );
	this.tfUser.addKeyListener( new KeyAdapter() {

	    @Override
	    public void keyPressed( KeyEvent e )
	    {
		if ( e.getKeyCode() == KeyEvent.VK_ENTER )
		{
		    loginAction();
		}
	    }
	} );
	this.pwf = new JPasswordField( "test" );
	this.pwf.addFocusListener( new FocusListener() {

	    @Override
	    public void focusLost( FocusEvent arg0 )
	    {
		// nothing.. :3
	    }

	    @Override
	    public void focusGained( FocusEvent arg0 )
	    {
		( (JTextField)arg0.getSource() ).selectAll();
	    }
	} );
	this.pwf.addKeyListener( new KeyAdapter() {

	    @Override
	    public void keyPressed( KeyEvent e )
	    {
		if ( e.getKeyCode() == KeyEvent.VK_ENTER )
		{
		    loginAction();
		}
	    }
	} );

	JButton bLogin = new JButton( "Anmelden" );
	bLogin.addActionListener( new ActionListener() {

	    @Override
	    public void actionPerformed( ActionEvent e )
	    {
		loginAction();
	    }
	} );
	JButton bCancel = new JButton( "Abbrechen" );
	bCancel.addActionListener( new ActionListener() {

	    @Override
	    public void actionPerformed( ActionEvent arg0 )
	    {
		System.exit( 0 );

	    }
	} );

	GridBagConstraints gbc;
	gbc = GBCUtils.getGBC( 0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 0, 0 ) );
	this.add( lUser, gbc );
	gbc = GBCUtils.getGBC( 0, 1, 2, 1, 1.0, 0.0, GridBagConstraints.HORIZONTAL );
	this.add( this.tfUser, gbc );
	gbc = GBCUtils.getGBC( 0, 2, 2, 1, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 0, 0 ) );
	this.add( lPW, gbc );
	gbc = GBCUtils.getGBC( 0, 3, 2, 1, 1.0, 0.0, GridBagConstraints.HORIZONTAL );
	this.add( this.pwf, gbc );
	gbc = GBCUtils.getGBC( 0, 4, 1, 1, 1.0, 0.0, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 0, 0 ) );
	this.add( bLogin, gbc );
	gbc = GBCUtils.getGBC( 1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 0, 0 ) );
	this.add( bCancel, gbc );

    }

    /**
     * creates exact one instance of this class
     * 
     * @param chessFrame
     *            reference to the ChessFrame-class
     * @return the instance of this class
     */
    public static LoginViewPanel getInstance( ChessFrame chessFrame )
    {
	if ( instance == null )
	{
	    instance = new LoginViewPanel( chessFrame );
	}
	return instance;
    }

    /**
     * invoked when the user wants to log in
     */
    private void loginAction()
    {
	String sep = "#";

	String name = this.tfUser.getText().trim();
	StringBuilder pw = new StringBuilder();
	pw.append( this.pwf.getPassword() );

	String loginData = Communication.login( name, pw.toString() );
	if ( loginData.split( sep )[0].equalsIgnoreCase( "s" ) ) // success
	{
	    this.chessFrame.onLogin( loginData );
	}
	else if ( loginData.split( sep )[0].equalsIgnoreCase( "f" ) ) // failed
	{
	    this.pwf.setText( "" );
	}
    }

}
